package com.briup.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.briup.bean.Product;
import com.briup.bean.User;
import com.briup.common.exception.UserException;
import com.briup.service.IreduceService;
import com.briup.service.impl.IreduceServiceImpl;

@WebServlet("/reduceSer")
public class reduceSer extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		String rid=request.getParameter("id");
		ServletContext sc=request.getServletContext();
		Set<Product> products=(Set<Product>) sc.getAttribute("products");
		Product pro=null;
		long id=Long.parseLong(rid);
		for(Product p:products) {
			if (p.getId()==id) {
				pro=p;
			}
		}
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");
		IreduceService service=new IreduceServiceImpl();
		try {
			service.pushReduce(user,pro);
		} catch (UserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String msg;
		if(user==null){
			msg="请先登陆";
		}else{
			msg="关注成功";
		}
		PrintWriter pw=response.getWriter();
		pw.write(msg);
		pw.flush();
		pw.close();
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
